﻿using Entities;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace Services
{
    public interface ICreateCode
    {
        string GetGroupInfoCode();
        string GetRegulatorCode();
        bool GroupInfoOrRegulatorCodeIsExists(string code);
        string GetInstallAddressCode();
        bool InstallAddressCodeExists(string code);
        string GetWireropeCode();
        bool WireropeCodeExists(string code);
        string GetHoisterCode();
        bool HoisterCodeExists(string code);
        string GetSupplierCode();
        bool SupplierCodeExists(string code);
        string GetProducerCode();
        bool ProducerCodeExists(string code);
        string GetWarehouseCode();
        bool WarehouseCodeExists(string code);
        string GetWarehousePositionCode();
        string GetBillCode(Bill_InOutType inOutType);
        int GetStockWireropeCode(int groupInfoID);
        bool WarehousePositionCodeExists(string code);
        string GetChangeWireropeCode();
        string GetStockWireropeKeepCode();
        string GetCheckWireropeCode();

    }

    public class CreateCode : ICreateCode
    {
        private readonly ISqlSugarClient _DB;
        private readonly ICurrentUser _currentUser;

        public CreateCode(ISqlSugarClient dB, ICurrentUser currentUser)
        {
            _DB = dB;
            _currentUser = currentUser;
        }

        #region 集团
        public string GetGroupInfoCode()
        {
            var code = "JT";

            var lastItem = _DB.Queryable<GroupInfo>().Where(x => x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }
        #endregion

        #region 监管部门
        public string GetRegulatorCode()
        {
            var code = "JG";

            var lastItem = _DB.Queryable<Regulator>().Where(x => x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }
        #endregion

        #region 获取集团或监管部门编码是否有重复的
        public bool GroupInfoOrRegulatorCodeIsExists(string code)
        {
            string sql = string.Empty;
            sql += $"SELECT SUM(T.Cnt) AS Cnt FROM ( ";
            sql += $"	SELECT COUNT(1) AS Cnt FROM glo.GroupInfo WHERE Code='{code}' ";
            sql += $"	UNION ALL ";
            sql += $"	SELECT COUNT(1) AS Cnt FROM glo.Regulator WHERE Code='{code}' ";
            sql += $") AS T ";

            return _DB.Ado.GetInt(sql) != 0;
        }
        #endregion

        #region 安装地点
        public string GetInstallAddressCode()
        {
            var code = "DD";

            var lastItem = _DB.Queryable<InstallAddress>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool InstallAddressCodeExists(string code)
        {
            return _DB.Queryable<InstallAddress>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 钢丝绳
        public string GetWireropeCode()
        {
            var code = "GSS";

            var lastItem = _DB.Queryable<Wirerope>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool WireropeCodeExists(string code)
        {
            return _DB.Queryable<Wirerope>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 提升机
        public string GetHoisterCode()
        {
            var code = "TSJ";

            var lastItem = _DB.Queryable<Hoister>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool HoisterCodeExists(string code)
        {
            return _DB.Queryable<Hoister>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 供应商
        public string GetSupplierCode()
        {
            var code = "GY";

            var lastItem = _DB.Queryable<Supplier>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool SupplierCodeExists(string code)
        {
            return _DB.Queryable<Supplier>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 生产商
        public string GetProducerCode()
        {
            var code = "ZZ";

            var lastItem = _DB.Queryable<Producer>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool ProducerCodeExists(string code)
        {
            return _DB.Queryable<Producer>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 仓库
        public string GetWarehouseCode()
        {
            var code = "CK";

            var lastItem = _DB.Queryable<Warehouse>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool WarehouseCodeExists(string code)
        {
            return _DB.Queryable<Warehouse>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 库位
        public string GetWarehousePositionCode()
        {
            var code = "KW";

            var lastItem = _DB.Queryable<WarehousePosition>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public bool WarehousePositionCodeExists(string code)
        {
            return _DB.Queryable<WarehousePosition>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code == code).Count() > 0;
        }
        #endregion

        #region 单据
        public string GetBillCode(Bill_InOutType inOutType)
        {
            string code = DateTime.Now.ToString("yyMMdd");
            switch (inOutType)
            {
                case Bill_InOutType.入库:
                    code = string.Format("I-{0}-", code);
                    break;
                case Bill_InOutType.出库:
                    code = string.Format("O-{0}-", code);
                    break;
            }

            var lastItem = _DB.Queryable<Bill>()
               .Where(x => x.Code.Contains(code) && x.InOutType == (int)inOutType && x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID)
               .OrderByDescending(x => x.Code)
               .First();

            if (!lastItem.IsEmpty())
            {
                var num = lastItem.Code.Right(3).ToInt();
                code += (++num).ToString().PadLeft(3, '0');
            }
            else
            {
                code += "001";
            }

            return code;
        }
        #endregion

        public int GetStockWireropeCode(int groupInfoID)
        {
            string code = DateTime.Now.ToString("yyMM");

            var info = _DB.Queryable<GroupInfo>().Where(x => x.GroupInfoID == groupInfoID).First();

            code = string.Format("{0}-{1}", info.Code, code);

            var lastItem = _DB.Queryable<BillDetail>()
               .Where(x => x.Code.Contains(code) && x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID)
               .OrderByDescending(x => x.Code)
               .First();

            if (!lastItem.IsEmpty())
            {
                return lastItem.Code.Right(4).ToInt();
            }
            else
            {
                return 1;
            }
        }

        public string GetChangeWireropeCode()
        {
            var code = "CH" + DateTime.Now.ToString("yyMMdd");

            var lastItem = _DB.Queryable<ChangeWirerope>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "001";
            }

            var num = lastItem.Code.Right(3).ToInt();
            return code += (++num).ToString().PadLeft(3, '0');
        }

        public string GetStockWireropeKeepCode()
        {
            var code = "BSWB" + DateTime.Now.ToString("yyMM");

            var lastItem = _DB.Queryable<StockWireropeKeep>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "0001";
            }

            var num = lastItem.Code.Right(4).ToInt();
            return code += (++num).ToString().PadLeft(4, '0');
        }

        public string GetCheckWireropeCode()
        {
            var code = "JC" + DateTime.Now.ToString("yyMM");

            var lastItem = _DB.Queryable<CheckWirerope>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.Code.Contains(code))
                .OrderByDescending(x => x.Code)
                .First();

            if (lastItem.IsEmpty())
            {
                return code += "0001";
            }

            var num = lastItem.Code.Right(4).ToInt();
            return code += (++num).ToString().PadLeft(4, '0');
        }
    }
}
